using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Farm.Inventory
{
    public class SlotUI : MonoBehaviour,IPointerClickHandler,IBeginDragHandler,IDragHandler,IEndDragHandler
    {
        [Header("组件获取")] 
        [SerializeField] private Image slotImage;

        [SerializeField] private TextMeshProUGUI amounttext;
        [SerializeField] public Image highLightSlot;
        [SerializeField] private Button button;
        [Header("格子类型")]
        public SlotType slotType;

        public bool isSelected;
        public int slotIndex;
    
        //物品信息
        public ItemDetails itemDetails;
        public int itemAmount;

        public InventoryUI inventoryUI => GetComponentInParent<InventoryUI>();

        public InventoryLocation Location
        {
            get
            {
                return slotType switch
                {
                    SlotType.Bag=>InventoryLocation.Player,
                    SlotType.Box=>InventoryLocation.Box,
                    _=>InventoryLocation.Player
                };
            }
        }

        private void Start()
        {
            isSelected = false;
            if (itemDetails.itemID==0)
            {
                UpdateEmptySlot();
            }
        }

        public void UpdateSlot(ItemDetails item, int amount)
        {
            itemDetails = item;
            slotImage.sprite = item.itemIcon;
            itemAmount = amount;
            amounttext.text = amount.ToString();
            slotImage.enabled = true;
            button.interactable = true;
        }

        public void UpdateEmptySlot()
        {
            if (isSelected)
            {
                isSelected = false;
                inventoryUI.UpdateSlotHighlight(-1);
                EventHandler.CallItemSelectedEvent(itemDetails,isSelected);
            }

            itemDetails = new ItemDetails();
            slotImage.enabled = false;
            slotImage.sprite = null;
            itemAmount=0;
            amounttext.text = string.Empty;
            button.interactable = false;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (itemAmount == 0) return; 
            isSelected=!isSelected;
            
            inventoryUI.UpdateSlotHighlight(slotIndex);

            if (slotType == SlotType.Bag)
            {
                EventHandler.CallItemSelectedEvent(itemDetails,isSelected);
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (itemAmount != 0)
            {
                inventoryUI.dragItem.enabled = true;
                inventoryUI.dragItem.sprite = slotImage.sprite;
                inventoryUI.dragItem.SetNativeSize();

                isSelected = true;
                inventoryUI.UpdateSlotHighlight(slotIndex);
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            inventoryUI.dragItem.transform.position = Input.mousePosition;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (itemAmount==0)
            {
                return;
            }
            inventoryUI.dragItem.enabled = false;
            if (eventData.pointerCurrentRaycast.gameObject != null)
            {
                if(eventData.pointerCurrentRaycast.gameObject.GetComponent<SlotUI>()==null)
                    return;
                var targetSlot = eventData.pointerCurrentRaycast.gameObject.GetComponent<SlotUI>();
                var targeIndex = targetSlot.slotIndex;
                if (slotType==SlotType.Bag&&targetSlot.slotType==SlotType.Bag)
                {
                    InventoryManager.Instance.SwapItem(slotIndex,targeIndex);
                }
                else if (slotType==SlotType.Shop&&targetSlot.slotType==SlotType.Bag)//买
                {
                    EventHandler.CallShowTradeUI(itemDetails,false);
                }
                else if (slotType==SlotType.Bag&&targetSlot.slotType==SlotType.Shop)//卖
                {
                    EventHandler.CallShowTradeUI(itemDetails,true);
                }
                else if (slotType != SlotType.Shop && targetSlot.slotType != SlotType.Shop &&
                         slotType != targetSlot.slotType)
                {
                    InventoryManager.Instance.SwapItem(Location,slotIndex,targetSlot.Location,targetSlot.slotIndex);
                }
                inventoryUI.UpdateSlotHighlight(-1);
            }
            // else
            // {
            //     if (slotType==SlotType.Bag)
            //     {
            //         EventHandler.CallUpdateInventoryUI(InventoryLocation.Player,InventoryManager.Instance.PlayerBag.itemList);
            //     }
            // }
        }
    }
}

